package com.lanswon.interceptor;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;





public class WSInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

	private String USERNAME="admin";
	private String PASSWORD="admin";
	
	public WSInterceptor(String name,String passWord) {
		super(Phase.PRE_PROTOCOL);
		this.USERNAME=name;
		this.PASSWORD=passWord;
		
	}

	@Override
	public void handleMessage(SoapMessage meg) throws Fault{
		@SuppressWarnings("deprecation")
		Document document=DOMUtils.createDocument();
		Element root=document.createElement("user");
		Element nodeName=document.createElement("name");
		nodeName.setTextContent(USERNAME);
		root.appendChild(nodeName);
		Element nodepw=document.createElement("passWord");
		nodepw.setTextContent(PASSWORD);
		root.appendChild(nodepw);
		List<Header> headers=meg.getHeaders();
		headers.add(new Header(new QName("user"),root));
	}

	

	public String getUSERNAME() {
		return USERNAME;
	}

	public void setUSERNAME(String uSERNAME) {
		USERNAME = uSERNAME;
	}

	public String getPASSWORD() {
		return PASSWORD;
	}

	public void setPASSWORD(String pASSWORD) {
		PASSWORD = pASSWORD;
	}

	public WSInterceptor() {
		super(Phase.PRE_PROTOCOL);
	}
	
}
